#region Using Directives

    using System;
    using System.Linq;
    using System.Text;

    using Csla;
    using Csla.Data;
    using Csla.Reflection;
    using Csla.Server;

    using MarketPulse.BusinessLogic.Core;
    using Production = MarketPulse.DataAccess.DataContextObjects.Production;

#endregion

namespace MarketPulse.DataAccess.Factory
{
    /// <summary>
    /// Abstracts the data access for a auditUserLogin away from a AuditUserLogin Business Object.
    /// </summary>
    public class AuditUserLoginFactory : ObjectFactory
    {
        #region Member Variables

            /// <summary>
            /// Property that stores the name of the class.
            /// </summary>
            private static readonly Type mClass = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;

            /// <summary>
            /// Property that stores the log4net logger for this class.
            /// </summary>
            private static readonly log4net.ILog mLog = log4net.LogManager.GetLogger(mClass);

        #endregion

        #region Methods

            #region Create Methods

                /// <summary>
                /// Method used to create and return a new AuditUserLogin object instance
                /// </summary>
                /// <returns>The newly created AuditUserLogin object</returns>
                [RunLocal]
                public AuditUserLogin Create()
                {
                    // Create a new instance of the object
                    var auditUserLogin = (AuditUserLogin)MethodCaller.CreateInstance(typeof(AuditUserLogin));

                    // Mark it as a new object
                    MarkNew(auditUserLogin);

                    //Check the business rules for this object
                    CheckRules(auditUserLogin);

                    return auditUserLogin;
                }

            #endregion

            #region Update Methods

                /// <summary>
                /// Method used to persist an altered AuditUserLogin object in the database 
                /// </summary>
                /// <param name="auditUserLogin">The AuditUserLogin object to persist</param>
                /// <returns>The AuditUserLogin object potentially altered by the act of persisting it</returns>
                [Transactional(TransactionalTypes.TransactionScope)]
                public AuditUserLogin Update(AuditUserLogin auditUserLogin)
                {
                    try
                    {
                        using (var manager = ContextManager<Production.MUM_dev>.GetManager(Database.BusinessSystem))
                        {
                            // Set up some local variables to hold the return values from the stored procedure calls
                            Guid? auditUserLoginGUID = null;
                            Nullable<DateTime> dateTime = DateTime.Now;

                            if (auditUserLogin.IsDirty)
                            {
                                if (auditUserLogin.IsNew)
                                {
                                    manager.DataContext.UspAuditUserLoginInsert
                                    (
                                        ref auditUserLoginGUID,
                                        auditUserLogin.AuditUserLoginResultGUID,
                                        auditUserLogin.UserGUID,
                                        auditUserLogin.ProfileTypeGUID,
                                        auditUserLogin.Username,
                                        auditUserLogin.IPAddress
                                    );

                                    // Load the updated values into the user object
                                    LoadProperty(auditUserLogin, AuditUserLogin.GUIDProperty, auditUserLoginGUID.Value);
                                    LoadProperty(auditUserLogin, AuditUserLogin.InsertDateTimeProperty, dateTime.Value);

                                }
                            }

                            // Make sure that CSLA knows this has changed since being instantiated
                            MarkOld(auditUserLogin);

                            return auditUserLogin;
                        }
                    }
                    catch (Exception ex)
                    {
                        mLog.Error(ex.ToString());
                        throw ex;
                    }
                }

            #endregion

        #endregion
    }
}